import {useLocalStorage} from "@/hooks/useLocalStorage"
import { THEMEMAP, defaultTheme } from '@/config'

type THEMES = keyof typeof THEMEMAP
/**
 * @description 切换主题方法
 * @author jinzz
 * @returns replaceTheme { () => void } 切换皮肤
 */
export function useTheme () {
  const replaceTheme = (newTheme ?: THEMES) => {
    const clear = () => {
      document.documentElement.classList.remove(THEMEMAP.dark)
      document.documentElement.classList.remove(THEMEMAP.light)
      document.documentElement.classList.remove(THEMEMAP.fruit)
    }
    if (!newTheme) {
      const { state: memoryTheme } = useLocalStorage('theme')
      newTheme = memoryTheme.value || defaultTheme
    }
    clear()
    document.documentElement.classList.add(THEMEMAP[newTheme as keyof typeof THEMEMAP])
    useLocalStorage('theme', newTheme)
  }
  return {
    replaceTheme
  }
}